package com.paimai.controller;

import com.alibaba.druid.util.StringUtils;
import com.paimai.pojo.Auctionuser;
import com.paimai.pojo.PageResult;
import com.paimai.service.AuctionuserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: VE
 * @DATE: 2022/10/10 20:29
 * 描述:
 */
@RestController
@RequestMapping("user")
public class AuctionuserController {

    private Logger logger = LoggerFactory.getLogger(AuctionuserController.class);
    @Autowired
    private AuctionuserService auctionuserService;

    @GetMapping("login")
    public PageResult login(String username,String password, HttpServletResponse rep){
        logger.info("用户登录");
        System.out.println(username);
        System.out.println(password);
        if(username==null || StringUtils.isEmpty(username)){
            return PageResult.none("不能为空");
        }
        Auctionuser loginUser = auctionuserService.login(username);
        if(loginUser==null|| !loginUser.getUserpassword().equals(password)){
            return PageResult.none("用户名或者密码错误");
        }
        Cookie cookie = new Cookie("loginUser",loginUser.getUsername());
        cookie.setMaxAge(3600);
        cookie.setPath("/");
        Cookie cookie1 = new Cookie("loginPwd",loginUser.getUserpassword());
        cookie1.setMaxAge(3600);
        cookie1.setPath("/");
        rep.addCookie(cookie);
        rep.addCookie(cookie1);
        return PageResult.success("登录成功",loginUser);
    }




}
